package com.mp.user.controller;

import com.mp.common.Code;
import com.mp.common.Result;
import com.mp.user.bean.User;
import com.mp.user.bean.UserDTO;
import com.mp.user.mapper.UserMapper;
import com.mp.user.service.UserService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 *
 * @author:
 * @date:
 */
@RestController
@Transactional(rollbackFor=Exception.class)
public class UserController {
    @Resource
    private UserMapper userMapper;
    @Resource
    private UserService userService;

    @GetMapping(value = "getUser")
    public Result getUser(){
        return Result.success(userService.selectList());
    }

    //传入json字符串
    @PostMapping("/getJson")
    public List<User> getJsonVal(@RequestBody List<User> user) {
        for(User user2 : user){
            System.out.println("user2 = " + user2);
        }
        return user;
    }

    //传入json字符串
    @PostMapping("/getJson2")
    public UserDTO getJsonVal2(@RequestBody UserDTO modal) {

        return modal;
    }




    //传入json对象
    @PostMapping("/getJson3")
    public User getJsonVal3(@RequestParam("id") String id,@RequestParam("name") String name) {
        User user = new User();
        user.setId(Long.parseLong(id));
        user.setName(name);
        return user;
    }
    @GetMapping("/test")
    public void testInsert(){
        User user = new User();
        user.setName("23wew");
        user.setAge(100);
        user.setEmail("123@qq.com");
        int insert = userMapper.insert(user);
        System.out.println(insert);
        System.out.println(user);
        int a=1/0;
    }


}
